package com.xls.dormitoryservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.dormitoryservice.service.HousingService;
import com.xls.dormitoryservice.vo.HousingQuery;
import com.xls.dormitoryservice.vo.HousingVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/dormitory/housing")
@Api(value = "入住离宿管理")
@Slf4j
public class HousingController {
    @Autowired
    private HousingService housingService;

    @Log(title = "入住离宿管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:housing:list')")
    @GetMapping("/getHousing/{id}")
    public Result<HousingVo> getHousing(@PathVariable String id) {
        return Result.success(housingService.getHousing(id));
    }

    @Log(title = "入住离宿管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:housing:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<HousingVo>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                        @RequestBody HousingQuery housingQuery) {
        return Result.success(housingService.getHousingVoPage(page, limit, housingQuery));
    }

    @Log(title = "入住离宿管理", businessType = "添加")
    @PreAuthorize("hasAnyAuthority('dormitory:housing:save')")
    @PostMapping("/save")
    public Result<HousingVo> save(@RequestBody HousingVo housingVo) {
        return Result.success(housingService.saveHousing(housingVo));
    }

    @Log(title = "入住离宿管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('dormitory:housing:update')")
    @PostMapping("/update")
    public Result<HousingVo> update(@RequestBody HousingVo housingVo) {
        return Result.success(housingService.updateHousing(housingVo));
    }

    @Log(title = "入住离宿管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('dormitory:housing:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<HousingVo> remove(@PathVariable String id) {
        return housingService.removeById(id) ? Result.success() : Result.error();
    }

    //@Log(title = "入住离宿管理", businessType = "入住/离宿")
    //@PostMapping("/studentHousing")
    //public Result<String> studentHousing(@RequestParam("roomId") String roomId,
    //                                     @RequestParam("type") String type) {
    //    housingService.studentHousing(roomId, type);
    //    return Result.success();
    //}

    @Log(title = "入住离宿管理", businessType = "入住/离宿")
    @PostMapping("/studentHousing")
    public Result<String> studentHousing(@RequestBody HousingVo housingVo) {
        housingService.studentHousing(housingVo);
        return Result.success();
    }
}

